package com.tjp.eight.ui.fragment.tabFind;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.WApplication;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.ui.fragment.rank.RankActivity;
import com.tjp.eight.ui.fragment.sub.SubActivity;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.BitmapUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.ToatUtils;
import com.tjp.eight.views.rcview.EndlessRecyclerOnScrollListener;
import com.tjp.eight.views.rcview.HeaderViewRecyclerAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Created by Administrator on 2017/2/13.
 */

public class FragmentFind extends BaseFragment<FindPresenter> implements FindView {

    private PullRefreshLayout prl_refresh_find;
    private EndlessRecyclerOnScrollListener endlessRecyclerOnScrollListener;
    private FindAdapter findAdapter;
    private RecyclerView rv_find;
    private HeaderViewRecyclerAdapter headerViewRecyclerAdapter;
    private TextView tvTitle;
    private ImageView ivNavLeft;
    private ImageView ivNavRight;
    private RelativeLayout rlHeadRight;
    private RelativeLayout rlHEadLeft;
    private boolean isRefresh = true;
    private int page = 1;
    private View headView;
    private BGABanner banner;
    private LinearLayout llMoreSub;
    private LinearLayout ll_more_find;
    private View footView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);

        if (contentView == null) {
            contentView = inflater.inflate(R.layout.frag_find_layout, container, false);
        }
        initViews();
        return contentView;
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    protected FindPresenter createPresenter() {
        return new FindPresenter(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (isRefresh) {
            presenter.loadData(getPostJson());
            isRefresh = false;
        }
    }

    @Override
    public void loadData(FindResultModel model) {
        if (model == null || model.data == null) return;
        if (isRefresh) {
            prl_refresh_find.setRefreshing(false);
            isRefresh = false;
            findAdapter.refreshData(model.data);
        } else {
            findAdapter.addData(model.data);
        }
        page = model.current + 1;
        if (findAdapter.getHeaderView() != null) {
            if (findAdapter.getItemCount() - 1 == model.total_items) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            } else {
                if (headerViewRecyclerAdapter.getFooterCount() == 0) {
                    headerViewRecyclerAdapter.addFooterView(footView);
                }
            }
        } else {
            if (findAdapter.getItemCount() == model.total_items) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            } else {
                if (headerViewRecyclerAdapter.getFooterCount() == 0) {
                    headerViewRecyclerAdapter.addFooterView(footView);
                }
            }
        }
    }

    @Override
    public void loadFail(String msg) {
        prl_refresh_find.setRefreshing(false);
        ToatUtils.toastCommentFail(getContext(), msg);
    }

    public String getPostJson() {
        FindPostModel postModel = new FindPostModel();
        postModel.page = page;
        postModel.uid = SharePreferencesUtils.getUid();
        postModel.loginkey = SharePreferencesUtils.getLoginKey();
        return new Gson().toJson(postModel);
    }

    void initViews() {
        prl_refresh_find = (PullRefreshLayout) contentView.findViewById(R.id.prl_refresh_find);
        rv_find = (RecyclerView) contentView.findViewById(R.id.rv_find);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        rv_find.setLayoutManager(linearLayoutManager);
        findAdapter = new FindAdapter(getContext());
        headView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_find_head, null, false);
        ll_more_find = (LinearLayout) headView.findViewById(R.id.ll_more_find);
        ll_more_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SubActivity.class));
            }
        });
        banner = (BGABanner) headView.findViewById(R.id.banner_find);
//        banner.setAdapter(new BGABanner.Adapter<ImageView, String>() {
//            @Override
//            public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
//                Glide.with(WApplication.getwApplication())
//                        .load(model)
//                        .placeholder(R.drawable.test_01)
//                        .error(R.drawable.test_01)
//                        .centerCrop()
//                        .dontAnimate()
//                        .into(itemView);
//            }
//        });
        List<View> views = new ArrayList<>();
        views.add(getPageView(R.drawable.tody_hot));
        views.add(getPageView(R.drawable.week_hot));
        views.add(getPageView(R.drawable.month_hot));
        banner.setData(views);
        banner.setDelegate(new BGABanner.Delegate<ImageView, String>() {
            @Override
            public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {
                Intent intent = new Intent(getActivity(), RankActivity.class);
                String type;
                if (position == 0) {
                    type = "day";
                } else if (position == 1) {
                    type = "week";
                } else {
                    type = "month";
                }
                intent.putExtra("rankType", "" + type);
                startActivity(intent);
            }
        });
        llMoreSub = (LinearLayout) headView.findViewById(R.id.ll_more_find);
        findAdapter.setHeaderView(headView);
        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(findAdapter);
        footView = LayoutInflater.from(getContext()).inflate(R.layout.swipe_refresh_footer, rv_find, false);
        headerViewRecyclerAdapter.addFooterView(footView);
        rv_find.setAdapter(headerViewRecyclerAdapter);
        endlessRecyclerOnScrollListener = new EndlessRecyclerOnScrollListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                page = currentPage;
                presenter.loadData(getPostJson());
            }
        };
        rv_find.addOnScrollListener(endlessRecyclerOnScrollListener);

        tvTitle = (TextView) contentView.findViewById(R.id.tv_title);
        ivNavLeft = (ImageView) contentView.findViewById(R.id.iv_head_left);
        ivNavRight = (ImageView) contentView.findViewById(R.id.iv_head_right);
        rlHEadLeft = (RelativeLayout) contentView.findViewById(R.id.rl_head_left);
        rlHeadRight = (RelativeLayout) contentView.findViewById(R.id.rl_head_right);
        tvTitle.setText("发现");
        rlHeadRight.setVisibility(View.VISIBLE);
        rlHEadLeft.setVisibility(View.VISIBLE);
        rlHEadLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.startSearch(getActivity());
            }
        });
        rlHeadRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SubActivity.class));
            }
        });
        prl_refresh_find.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                endlessRecyclerOnScrollListener.reSet();
                isRefresh = true;
                page = 1;
                presenter.loadData(getPostJson());
            }
        });
    }

    private View getPageView(@DrawableRes int resid) {
        ImageView imageView = new ImageView(getContext());
        imageView.setImageBitmap(BitmapUtils.getBitmap(getContext(), resid));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return imageView;
    }
}
